<template>
  <el-dialog
    title="增加费用类别"
    :visible.sync="visible"
    :close-on-click-modal="false"
    width="460px"
  >
    <el-form
      ref="form"
      :model="form" 
      label-width="120px"
      style="width: 400px;"
    >
      <el-form-item v-if="parentCategory.id != 0" label="父类别">
        {{parentCategory.name}}
      </el-form-item>
      <el-form-item prop="name" label="名称" :rules="[{required: true, message: ' '}]">
        <el-input v-model.trim="form.name" autofocus></el-input>
      </el-form-item>
      <el-form-item prop="price" label="每月价格">
        <el-input-number v-model="form.price" :precision="2" :min="0" :controls="false" />
        <span>空或0表示分类目录</span>
      </el-form-item>
      <el-form-item prop="note" label="描述">
        <el-input type="textarea" :rows="8" v-model.trim="form.note" :maxlength="1000" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" @click="onSubmit" :loading="submitting">确定</el-button>
      <el-button type="default" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>


<script>
export default {
  data() {
    return {
      visible: false,
      form: {
        parentId: 0,
        name: '',
        price: '',
        note: ''
      },
      parentCategory: {},
      submitting: false
    }
  },
  methods: {
    async show(options) {
      this.options = options;
      this.visible = true;
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.parentCategory = options.category;
      this.form.resthomeId = options.category.resthomeId;
      this.form.parentId = options.category.id;
    },
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;
        this.submitting = true;
        let form = {...this.form};
        if (!form.price) form.price = '';
        const ret = await this.$http.post('/api/community/bedChargeCategory', form);
        this.submitting = false;
        if (ret.success) {
          this.$message.success('增加成功');
          this.visible = false;
          this.options.onSuccess(data);
        }
      });
    },
  }
}
</script>